package net.funtip.jdbc.driver;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.logging.Logger;

import net.funtip.jdbc.JDBCLogger;

public class FTConnection implements Connection {

	protected Connection realConnection;

	protected static JDBCLogger jdbcLogger = null;
	protected static Logger log = Logger.getLogger(FTConnection.class.getName());

	public Connection realConnection() {
		return realConnection;
	}

	public FTConnection(Connection connection) {
		realConnection = connection;
	}

	public JDBCLogger getJdbcLogger() {
		return JDBCLogger.getInstance();
	}
	
	public Statement createStatement() throws SQLException {
		return new FTStatement(realConnection.createStatement(), this);
	}

	public Statement createStatement(
			int resultSetType,
			int resultSetConcurrency)
	throws SQLException {
		return new FTStatement(
				realConnection.createStatement(resultSetType, resultSetConcurrency),
				this);
	}

	public CallableStatement prepareCall(String sql) throws SQLException {
		return new FTCallableStatement(
				realConnection.prepareCall(sql),
				this,
				sql);
	}

	public CallableStatement prepareCall(
			String sql,
			int resultSetType,
			int resultSetConcurrency)
	throws SQLException {
		return new FTCallableStatement(
				realConnection.prepareCall(
						sql,
						resultSetType,
						resultSetConcurrency),
						this,
						sql);
	}

	public PreparedStatement prepareStatement(String sql) throws SQLException {
		FTPreparedStatement pstmt = new FTPreparedStatement(
				realConnection.prepareStatement(sql),
				this,
				sql);
		getJdbcLogger().incrementPrepareCount(sql);
		return pstmt;
	}

	public PreparedStatement prepareStatement(
			String sql,
			int resultSetType,
			int resultSetConcurrency)
	throws SQLException {
		return new FTPreparedStatement(
				realConnection.prepareStatement(
						sql,
						resultSetType,
						resultSetConcurrency),
						this,
						sql);
	}

	public DatabaseMetaData getMetaData() throws SQLException {
		return new FTDatabaseMetaData(realConnection.getMetaData(), this);
	}

	public void clearWarnings() throws SQLException {
		realConnection.clearWarnings();
	}

	public void close() throws SQLException {
		if (!realConnection.isClosed()) {
			realConnection.close();
		}
	}

	public void commit() throws SQLException {
		realConnection.commit();
	}

	public boolean getAutoCommit() throws SQLException {
		return realConnection.getAutoCommit();
	}

	public String getCatalog() throws SQLException {
		return realConnection.getCatalog();
	}

	public int getTransactionIsolation() throws SQLException {
		return realConnection.getTransactionIsolation();
	}

	public Map<String, Class<?>> getTypeMap() throws SQLException {
		return realConnection.getTypeMap();
	}

	public SQLWarning getWarnings() throws SQLException {
		return realConnection.getWarnings();
	}

	public boolean isClosed() throws SQLException {
		return realConnection.isClosed();
	}

	public boolean isReadOnly() throws SQLException {
		return realConnection.isReadOnly();
	}

	public String nativeSQL(String sql) throws SQLException {
		return realConnection.nativeSQL(sql);
	}

	public void rollback() throws SQLException {
		realConnection.rollback();
	}

	public void setAutoCommit(boolean autoCommit) throws SQLException {
		realConnection.setAutoCommit(autoCommit);
		log.finest("setting autoCommit to:"+autoCommit);
	}

	public void setCatalog(String catalog) throws SQLException {
		realConnection.setCatalog(catalog);
	}

	public void setReadOnly(boolean readOnly) throws SQLException {
		realConnection.setReadOnly(readOnly);
	}

	public void setTransactionIsolation(int level) throws SQLException {
		realConnection.setTransactionIsolation(level);
	}

	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		realConnection.setTypeMap(map);
	}

	public void setHoldability(int holdability) throws SQLException {
		realConnection.setHoldability(holdability);
	}

	public int getHoldability() throws SQLException {
		return realConnection.getHoldability();
	}

	public Savepoint setSavepoint() throws SQLException {
		return realConnection.setSavepoint();
	}

	public Savepoint setSavepoint(String name) throws SQLException {
		return realConnection.setSavepoint(name);
	}

	public void rollback(Savepoint savepoint) throws SQLException {
		realConnection.rollback(savepoint);
	}

	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
		realConnection.releaseSavepoint(savepoint);
	}

	public Statement createStatement(
			int resultSetType,
			int resultSetConcurrency,
			int resultSetHoldability)
	throws SQLException {
		return realConnection.createStatement(
				resultSetType,
				resultSetConcurrency,
				resultSetHoldability);
	}

	public PreparedStatement prepareStatement(
			String sql,
			int resultSetType,
			int resultSetConcurrency,
			int resultSetHoldability)
	throws SQLException {
		return realConnection.prepareStatement(
				sql,
				resultSetType,
				resultSetConcurrency,
				resultSetHoldability);
	}

	public CallableStatement prepareCall(
			String sql,
			int resultSetType,
			int resultSetConcurrency,
			int resultSetHoldability)
	throws SQLException {
		return realConnection.prepareCall(
				sql,
				resultSetType,
				resultSetConcurrency,
				resultSetHoldability);
	}

	public PreparedStatement prepareStatement(
			String sql,
			int autoGeneratedKeys)
	throws SQLException {
		return realConnection.prepareStatement(sql, autoGeneratedKeys);
	}

	public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
	throws SQLException {
		return realConnection.prepareStatement(sql, columnIndexes);
	}

	public PreparedStatement prepareStatement(String sql, String[] columnNames)
	throws SQLException {
		return realConnection.prepareStatement(sql, columnNames);
	}

	public Array createArrayOf(String typeName, Object[] elements)
			throws SQLException {
		return realConnection.createArrayOf(typeName, elements);
	}

	public Blob createBlob() throws SQLException {
		return realConnection.createBlob();
	}

	public Clob createClob() throws SQLException {
		return realConnection.createClob();
	}

	public NClob createNClob() throws SQLException {
		return realConnection.createNClob();
	}

	public SQLXML createSQLXML() throws SQLException {
		return realConnection.createSQLXML();
	}

	public Struct createStruct(String typeName, Object[] attributes)
			throws SQLException {
		return realConnection.createStruct(typeName, attributes);
	}

	public Properties getClientInfo() throws SQLException {
		return realConnection.getClientInfo();
	}

	public String getClientInfo(String name) throws SQLException {
		return realConnection.getClientInfo(name);
	}

	public boolean isValid(int timeout) throws SQLException {
		return realConnection.isValid(timeout);
	}

	public void setClientInfo(Properties properties)
			throws SQLClientInfoException {
		realConnection.setClientInfo(properties);
	}

	public void setClientInfo(String name, String value)
			throws SQLClientInfoException {
		realConnection.setClientInfo(name, value);
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return realConnection.isWrapperFor(iface);
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		return realConnection.unwrap(iface);
	}

	public void setSchema(String schema) throws SQLException {
		// TODO implement when using JDK 1.7
	}

	public String getSchema() throws SQLException {
		// TODO implement when using JDK 1.7
		return null;
	}

	public void abort(Executor executor) throws SQLException {
		// TODO implement when using JDK 1.7
	}

	public void setNetworkTimeout(Executor executor, int milliseconds)
			throws SQLException {
		// TODO implement when using JDK 1.7
	}

	public int getNetworkTimeout() throws SQLException {
		// TODO implement when using JDK 1.7
		return 0;
	}

	public void onClosePreparedStatement(String lastSql) {
		getJdbcLogger().incrementCloseCount(lastSql);
	}
	

}
